package com.example.android.bluetoothchat.uitls;

import com.google.gson.Gson;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Administrator on 2017/4/23 0023.
 */

public class StringUtils {

    public static final String KEY_ENTER = "enter";
    public static final String KEY_OUT = "out";
    public static final String MSG_DEIVDER = "==";

    private static String DATE_TIME_FORMAT = "HH:mm:ss";

    private static String DATE_TIME_FORMAT_YY_MM = "yyyy-MM-dd HH:mm:ss";

    public static String getDateTime() {
        SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_TIME_FORMAT);
        return dateFormat.format(new Date());
    }

    /**
     * 格式化时间显示时分秒
     *
     * @param time
     * @return
     */
    public static String getDateTime(long time) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_TIME_FORMAT);
        return dateFormat.format(new Date(time));
    }

    /**
     * 格式化时间显示年月日时分秒
     *
     * @param time
     * @return
     */
    public static String getDateTimeWithYM(long time) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_TIME_FORMAT_YY_MM);
        return dateFormat.format(new Date(time));
    }

    /**
     * 计算时间差
     *
     * @param starTime 开始时间
     * @param endTime  结束时间
     * @return 返回时间差
     */
    public static String getTimeDifference(long starTime, long endTime) {
        String timeString = "";
        long diff = endTime - starTime;

        long day = diff / (24 * 60 * 60 * 1000);
        long hour = (diff / (60 * 60 * 1000) - day * 24);
        long min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
        long s = (diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
        long ms = (diff - day * 24 * 60 * 60 * 1000 - hour * 60 * 60 * 1000
                - min * 60 * 1000 - s * 1000);
        // System.out.println(day + "天" + hour + "小时" + min + "分" + s +
        // "秒");
        long hour1 = diff / (60 * 60 * 1000);
        String hourString = hour1 + "";
        long min1 = ((diff / (60 * 1000)) - hour1 * 60);
        if (hour1 <= 0 && min1 <= 0) {
            timeString = s + "秒";
        } else if (hour1 <= 0 && min1 > 0) {
            timeString = min1 + "分" + s + "秒";
        } else {
            timeString = hour1 + "小时" + min1 + "分" + s + "秒";
        }

        // System.out.println(day + "天" + hour + "小时" + min + "分" + s +
        // "秒");
        return timeString;

    }

    /**
     * 计算停车费
     *
     * @param startTime
     * @param endTime
     * @return
     */
    public static long calcuteNeedpay(long startTime, long endTime) {
        long diff = endTime - startTime;

        long hour = diff / (60 * 60 * 1000);

        long min = (diff % (60 * 60 * 1000)) / (60 * 1000);

        long sec = ((diff % (60 * 60 * 1000)) % (60 * 1000)) / 1000;

        if (hour == 0 && (min > 0 || sec > 0)) {
            hour = 1;
        }
        long money = 5 + (hour - 1) * 3;
        return money;
    }

    /**
     * 保存记录到本地
     *
     * @param key 保存的key用于取出数据用
     * @param value 保存数据
     */
    public static void saveObject(String key, Serializable value) {
        Gson gson = new Gson();
        String json = gson.toJson(value);
        ShareUtils.putString(key, json);
        //ACache.get(context).put(key, value);
    }

    /**
     * 取出本地保存的数据
     *
     * @param key 保存的key
     * @param tClass 保存的类型
     * @param <T>
     * @return
     */
    public static <T> Object getObject(String key, Class<T> tClass) {
        Gson gson = new Gson();
        String json = ShareUtils.getString(key, "");
        return gson.fromJson(json, tClass);
        //return ACache.get(context).getAsObject(key);
    }

}
